/**
 * px和rem单位互转
 * @param content {string} 样式表的内容
 * @param unit {string}  需要替换的单位名称
 * @param rule {string}
 *    单位换算规则，多少rem = px || px = rem
 *    例如：当rule为100时即表示 1rem = 100px
 *@param updateUnit
 */
/* eslint-disable */
// 单位换算
 function unitConversion({num, unit, updateUnit, rule = 100}) {
  let res = null;
  if (unit === 'px' && updateUnit === 'rem') {
    res = num / rule;
  } else if (unit === 'rem' && updateUnit === 'px') {
    res = num * rule;
  }
  return  ' ' + res + updateUnit;
}
 export default ({content, unit, rule, updateUnit}) => {
  rule = rule || 100;
  if (!rule) {return content; }
  const reg = {
    px : /[\:]*[\s]*[-]*[\s]*[0-9]*[\.]*[0-9]*px/g,
    rem: /[\:]*[\s]*[-]*[\s]*[0-9]*[\.]*[0-9]*rem/g,
    number: /[0-9]*[\.]*[0-9]*/g
  };
  // 获取到所有匹配的内容
  const res = content.match(reg[unit]);
  // 传进来的是正则匹配的结构内容
  function getRightData(arr = []) {
    const result = [];
    for (let  i = 0; i < arr.length; i++) {
      result.push(Number(arr[i].replace(new RegExp(' ', 'gm'), '').replace(':', '').replace(unit, '')));
    }
    return result;
  }

  const rightData = getRightData(res);

  for (let i = 0; i < rightData.length; i++) {
    let txt = unitConversion({
      num: rightData[i],
      unit,
      updateUnit,
      rule,
    });
    if (res[i].indexOf(':') >= 0) {
      txt = ': ' + txt;
    }
    content = content.replace(res[i], txt);
  }
  return content;
 };
